//
//  ViewController.swift
//  SelectMonth
//
//  Created by WB Zhou on 2017/2/27.
//  Copyright © 2017年 aowang. All rights reserved.
//

import UIKit

class SelectMonthView: UIView {

    var monthD = [String]()
    var yearD = [String]()

    var fetchData: ((String) -> Void)?

    var contentView = UIView()
    var picker = UIPickerView()
    lazy var cancal: UIButton = {
        let btn:UIButton = UIButton()
        btn.backgroundColor = Theme.LIGHT_GREY
        btn.setTitle("取消", for: UIControl.State())
        return btn
    }()
    lazy var confirm: UIButton = {
        let btn:UIButton = UIButton()
        btn.backgroundColor = Theme.LIGHT_BLUE
        btn.setTitle("确认", for: UIControl.State())
        return btn
    }()
    lazy var headerView: UILabel = {
        let label :UILabel = UILabel()
        label.textAlignment = .center
        label.textColor = Theme.LIGHT_BLUE
        label.text = "选择月份"
        return label
    }()

    init() {
        super.init(frame: UIScreen.main.bounds)

        createCollectionViewWithConfig()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func createCollectionViewWithConfig() {

        for i in 1..<13 {
            i > 9 ? monthD.append("\(i)") : monthD.append("0\(i)")
        }

        for i in 1900...2100 {
            yearD.append(String(i))
        }

        backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7)
        addSubview(contentView)

        contentView.backgroundColor = UIColor.white
        contentView.frame = CGRect(x: 24, y: UIScreen.main.bounds.height / 3, width: UIScreen.main.bounds.width - 48, height: UIScreen.main.bounds.height / 3)
        headerView.frame = CGRect(x: 0, y: 0, width: contentView.frame.width, height: 30)
        picker.frame = CGRect(x: 0, y: 30, width: contentView.frame.width, height: contentView.frame.height - 70)
        confirm.frame = CGRect(x: 0, y: contentView.frame.height - 40, width: contentView.frame.width / 2, height: 40)
        cancal.frame = CGRect(x: contentView.frame.width / 2, y: contentView.frame.height - 40, width: contentView.frame.width / 2, height: 40)

        let line = UIView(frame: CGRect(x: 8, y: 30, width: contentView.frame.width - 16, height: 2))
        line.backgroundColor = Theme.LIGHT_GREY
        contentView.addSubview(line)

        confirm.addTarget(self, action: #selector(clickBtn), for: .touchUpInside)
        cancal.addTarget(self, action: #selector(clickCancleBtn), for: .touchUpInside)

        picker.dataSource = self
        picker.delegate = self

        picker.selectRow(117, inComponent: 0, animated: true)
        picker.selectRow(3, inComponent: 1, animated: true)

        contentView.addSubview(headerView)
        contentView.addSubview(picker)
        contentView.addSubview(cancal)
        contentView.addSubview(confirm)

        contentView.layer.cornerRadius = 10
        contentView.layer.masksToBounds = true
    }

    @objc fileprivate func clickBtn() {

        let a = picker.selectedRow(inComponent: 0)
        let b = picker.selectedRow(inComponent: 1)
        let c = yearD[a] + "-" + monthD[b]
        if let fetchData = self.fetchData {
            fetchData(c)
        }

        KSLog(c)
        removeFromSuperview()
    }

    @objc fileprivate func clickCancleBtn() {
        KSLog("clickCancleBtn")
        removeFromSuperview()
    }

    func show() {
        let rv = UIApplication.shared.keyWindow! as UIWindow
        rv.addSubview(self)
    }

}

extension SelectMonthView: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return yearD.count
        } else {
            return monthD.count
        }
    }

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let label :UILabel = UILabel()
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 20)
        label.textColor = Theme.DARK_GREY
        if component == 0 {
            label.text = yearD[row]
        } else {
            label.text = monthD[row]
        }
        return label
    }

    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let attrStr = NSMutableAttributedString()
        attrStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: NSRange(location: 0, length: 1))
        return attrStr
    }
}
